import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';
import { User } from 'src/module/user/entities/user.entity';
import { Permission } from 'src/module/permission/entities/permission.entity';

@Entity()
export class Role {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, /**唯一约束code */ comment: '角色code' })
  code: string;

  @Column({ unique: true, /** 唯一约束name */ comment: '角色名称' })
  name: string;

  @Column({ default: true, comment: '是否启用' })
  enable: number;

  /** 用户角色之间多对多关系 */
  @ManyToMany(() => User, (user) => user.roles, {
    createForeignKeyConstraints: false,
  })
  users: User[];

  /** 角色权限之间是多对多的关系 */
  @ManyToMany(() => Permission, (permission) => permission.roles)
  @JoinTable({
    name: 'role_permissions_permission',
  })
  permissions: Permission[];
}
